<template>
    <div>
        <div style="padding:20px">
            <el-button type="primary" @click="uptext">提交</el-button>
            {{$store.state.user.info.profile.name}}
        </div>
        <div class="titlenav" style="padding:20px 0;">
            <el-form ref="form" :model="form" label-width="80px">
            <el-row :gutter="20">
            <el-col :span="18">
                    <el-input v-model="form.title" placeholder="标题"></el-input>
            </el-col>
            <el-col :span="6">
                    <el-select v-model="form.idChannel" placeholder="选择类型" v-if="channellistview">
                        <el-option v-for="i in channellistview" :label="i.name" :value="i.id"></el-option>
                    </el-select>
            </el-col>
            </el-row>
        </el-form>
        </div>
        <div style="background:#fff;">
            <div ref="widthcontent" class="widthcontent"></div>
        </div>
    </div>
</template>
<script>
import wangeditor from 'wangeditor'
import {http,channellist,file,article} from '../../api/api'
export default {
    data(){
        return{
            form:{
                idChannel:'',
                title:''
            },
            channellistview:'',
            editor:''
        }
    },
    mounted(){
        //开启动画
            const loading = this.$loading({
                lock: true,
                text: 'Loading',
                background: 'rgba(0, 0, 0, 0.7)'
			});
			
        this.editor = new wangeditor(this.$refs.widthcontent)
        this.editor.customConfig.showLinkImg = false
        this.editor.customConfig.uploadImgServer = '/upload'
        this.editor.customConfig.uploadImgMaxLength = 1
        this.editor.customConfig.customUploadImg =(files, insert)=>{
            // files 是 input 中选中的文件列表
            // insert 是获取图片 url 后，插入到编辑器的方法
               let f = files[0]
               let d = new FormData
               d.append('file',f)
               this.$http.post(http+file,d,{
                   headers:{
                       'constnet-type':'multipart/form-data',
                       'Authorization':localStorage.token
                   }
               }).then((data)=>{
                if(data.data.msg == '成功') {
                    insert(data.data.data.realFileName)
				} else {
					this.$message.error(data.data.msg);
				}
               },(err)=>{
                   this.$message.error(err.data.message);
               })
        }
        this.editor.create();
        this.getchannellist()
        //结束动画
                setTimeout(() => {
                    loading.close();
                }, 1000);
    },
    methods:{
        uptext(){
            //提交文章
            let json ={
                author:this.$store.state.user.info.profile.name,
                content:this.editor.txt.html().replace(/\%/g,'%25'),
                idChannel:this.form.idChannel,
                title:this.form.title
            }
            var type = true
            for(var i in json){
                if(json[i]==''){
                    type = false
                }
            }
            if(type){
                this.$http.post(http+article,JSON.stringify(json)).then((data)=>{
                    if(data.data.msg == '成功') {
                        this.$message({
                        message: '提交成功了',
                        type: 'success'
                        });
                        console.log(data.data)
                    } else {
                        this.$message.error(data.data.msg);
                    }
                    console.log(data.data)
                },(err)=>{
                    this.$message.error(err.data.message);
                })
            }else{
                this.$message.error('请补全数据');
            }
            
            console.log(json)
        },
        getchannellist(){
            //获取分类
            this.$http.get(http+channellist).then((data)=>{
                if(data.data.msg == '成功') {
					this.channellistview = data.data.data
				} else {
					this.$message.error(data.data.msg);
				}
                console.log(data.data)
            },(err)=>{
                this.$message.error(err.data.message);
            })
        }
    }
}
</script>
<style>
    .widthcontent{
        position: relative;
        z-index: 1;
    }
    .titlenav{
        position: relative;
        z-index: 2;
    }
</style>